<template>
  <div class="admin-categories">
    <header class="header">
      <div class="container">
        <div class="logo">
          <h1>拼多多 - 分类管理</h1>
        </div>
        <nav class="nav">
          <router-link to="/">返回首页</router-link>
          <router-link to="/admin/products">商品管理</router-link>
          <router-link to="/admin/users">用户管理</router-link>
          <router-link to="/admin/cart">购物车管理</router-link>
          <router-link to="/admin/categories">分类管理</router-link>
        </nav>
      </div>
    </header>

    <div class="container">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>分类管理</span>
            <el-button type="primary" @click="handleAdd">添加分类</el-button>
          </div>
        </template>

        <el-table :data="categories" style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="name" label="分类名称" />
          <el-table-column prop="description" label="描述" />
          <el-table-column prop="created_at" label="创建时间" />
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 添加/编辑对话框 -->
      <el-dialog
        v-model="dialogVisible"
        :title="dialogTitle"
        width="500px"
      >
        <el-form :model="form" label-width="80px">
          <el-form-item label="分类名称">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description" type="textarea" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '../../utils/request'

const categories = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('添加分类')
const form = ref({
  id: null,
  name: '',
  description: ''
})

const loadCategories = async () => {
  try {
    const res = await request.get('/categories')
    categories.value = res.data || []
  } catch (error) {
    ElMessage.error('加载分类失败')
  }
}

const handleAdd = () => {
  dialogTitle.value = '添加分类'
  form.value = {
    id: null,
    name: '',
    description: ''
  }
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑分类'
  form.value = { ...row }
  dialogVisible.value = true
}

const handleSubmit = async () => {
  try {
    if (form.value.id) {
      await request.put(`/categories/${form.value.id}`, form.value)
      ElMessage.success('更新成功')
    } else {
      await request.post('/categories', form.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    loadCategories()
  } catch (error) {
    ElMessage.error(form.value.id ? '更新失败' : '添加失败')
  }
}

const handleDelete = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除这个分类吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await request.delete(`/categories/${id}`)
    ElMessage.success('删除成功')
    loadCategories()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  loadCategories()
})
</script>

<style scoped>
.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 15px 0;
  margin-bottom: 20px;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo h1 {
  color: #E02E24;
  font-size: 20px;
}

.nav {
  display: flex;
  gap: 20px;
}

.nav a {
  text-decoration: none;
  color: #333;
}

.nav a.router-link-active {
  color: #E02E24;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

